﻿using System.Collections.Generic;
using Equation.Enterprise.Framework.Domain.Model;

namespace Equation.Enterprise.Framework.Web.Model
{
    public interface IViewModelMapper<TEntity, TEntityIdType>
        where TEntity : IEntityWithTypedId<TEntityIdType>
    {
        void MapFromDomainModel(TEntity model);
        void MapToDomainModel(TEntity domainModel);
    }

    public interface IViewModelListMapper<TEntity, TEntityIdType>
        where TEntity : IEntityWithTypedId<TEntityIdType>
    {
        void MapFromListDomainModel(IList<TEntity> models);
        void MapToListDomainModel(IList<TEntity> models);
    }

    public interface IViewModelMapper<TEntity> : IViewModelMapper<TEntity, int>
        where TEntity : IEntityWithTypedId<int>
    {

    }

    public interface IViewModelListMapper<TEntity> : IViewModelListMapper<TEntity, int>
        where TEntity : IEntityWithTypedId<int>
    {

    }

    public abstract class ViewModelMapper<TViewModel, TDomainModel>
                                    where TViewModel : new()
                                    where TDomainModel : new()
    {
        public abstract void MapDomainToView(TDomainModel domainModel, TViewModel viewModel);
        public abstract void MapViewToDomain(TViewModel viewModel, TDomainModel domainModel);
        
        public void ReMap(TViewModel viewModel)
        {
            TDomainModel domainModel = new TDomainModel();

            MapViewToDomain(viewModel, domainModel);
            MapDomainToView(domainModel, viewModel);
        }
    }

    public abstract class ListViewModelMapper<TListViewItem, TDomainModel>
        : ViewModelMapper<GenericListViewModel<TListViewItem>, List<TDomainModel>>
    {
        public abstract override void MapDomainToView(List<TDomainModel> domainModel, GenericListViewModel<TListViewItem> viewModel);
        public abstract override void MapViewToDomain(GenericListViewModel<TListViewItem> viewModel, List<TDomainModel> domainModel);
    }
}
